#include "LogEventImp.h"
#include <stdarg.h>

namespace log
{


LogEventImp::LogEventImp(Logger::ptr ptrLogger, LogLevel::ELevel eLevel, uint32_t tid, const std::string& strTime
            , const std::string& strFile, const std::string& strFunc, uint32_t nLine)
    : m_ptrLogger(ptrLogger)
{
    m_ptrEvent = std::make_shared<LogEvent>(eLevel, tid, strTime, strFile, strFunc, nLine);
}

LogEventImp::~LogEventImp()
{
    m_ptrLogger->log(m_ptrEvent);
}

std::ostringstream& LogEventImp::stream()
{
    return m_ptrEvent->stream();
} 

void LogEventImp::format(const char* fmt, ...)
{
    va_list ap;

    size_t size = 0;
    char* buf = nullptr;
#ifdef WIN32
    va_start(ap, fmt);
    size = vsnprintf(buf, size, fmt, ap) 
    va_end(ap);
    if (size > 0)
    {
        buf = (char*)malloc(++size);
        va_start(ap, fmt);
        size = ::vsnprintf(buf, size, fmt, vl);
        va_end(ap);
    }
#else
    va_start(ap, fmt);
    size = ::vasprintf(&buf, fmt, ap);
    va_end(ap);
#endif
    if (buf)
    {
        m_ptrEvent->stream() << buf;
        free(buf);
        buf = nullptr;
    }
}
} // namespace log
